<template>
  <div class="app-container">
    <div class="filter-container">
      <!-- <el-input :placeholder="`${$t('common.table.id')}/${$t('member.cellphone')}/${$t('member.name')}`" v-model="listQuery.title" style="width: 200px;" class="filter-item"
        clearable @keyup.enter.native="handleFilter" />
      <span class="filter-item">
        <el-date-picker v-model="listQuery.timeInterval" :picker-options="pickerOptions" :range-separator="$t('common.to')" :start-placeholder="$t('date_picker.start_placeholder')"
          :end-placeholder="$t('date_picker.end_placeholder')" type="datetimerange" align="right" value-format="yyyy-MM-dd HH:mm:ss" />
      </span> -->
      <el-radio-group
        v-model="listQuery.type"
        class="filter-item"
        @change="handleFilter"
      >
        <el-radio-button label="news">新闻公告</el-radio-button>
        <el-radio-button label="notice">其他内容</el-radio-button>
      </el-radio-group>
      <!-- <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">{{ $t('common.search') }}</el-button> -->
      <!-- <el-button v-permission="$store.jurisdiction.MemberExport" :loading="formLoading" class="filter-item" style="margin-left: 10px;float:right;" icon="el-icon-download"
        @click="handleExport">{{ $t('common.export') }}</el-button>-->
      <!-- <router-link :to="'NewsCreate'"> -->
      <el-button
        class="filter-item"
        style="margin-left: 10px;float:right;"
        type="primary"
        icon="el-icon-edit"
        @click="handleAdd"
        >{{ $t("common.add") }}</el-button
      >
      <!-- </router-link> -->
      <!-- <el-button class="filter-item" style="margin-left: 10px;float:right;" type="primary" icon="el-icon-edit" @click="handleCreate">{{ $t('common.add') }}</el-button> -->
      <!--<el-button v-permisssion="jurisdiction.usersListExport" v-waves :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">{{ $t('usuel.export') }}</el-button>-->
      <!-- <el-form v-if="state === 0" :inline="true" :model="listQuery" class="demo-form-inline">
        <el-form-item :label="$t('good.search.form.title.label')">
          <el-input v-model="listQuery.title" :placeholder="$t('good.search.form.title.placeholder')" clearable @keyup.enter.native="handleFilter" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleFilter">{{ $t('common.search') }}</el-button>
        </el-form-item>
      </el-form> -->
    </div>
    <!-- <div class="operation" style="margin-bottom: 10px;">
      <el-button size="mini" @click="handleCheckAllChange">{{ $t('common.check_all') }}/{{ $t('common.inverse') }}</el-button>
      <el-button :loading="formLoading" size="mini" type="danger" @click="handleAllDelete()">
        {{ $t('common.delete') }}</el-button>
    </div> -->
    <p>新闻类 实时（添加、编辑）更新，公告类（介绍）尽量不要添加只编辑</p>

    <el-table
      v-loading="listLoading"
      ref="multipleTable"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" fixed="left" />
      <!-- <el-table-column label="序号" type="index" width="50" align="center">
          <template slot-scope="scope">
            {{ total - ((listQuery.page - 1) * listQuery.limit + scope.$index) }}
          </template>
        </el-table-column> -->

      <el-table-column label="文章id" sortable="custom" prop="id" width="100">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>

      <el-table-column label="标题">
        <template slot-scope="scope">
          <span>{{ scope.row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column label="类型" prop="type">
        <template slot-scope="scope">
          {{ scope.row.type }}
        </template>
      </el-table-column>
      <!-- <el-table-column :label="$t('good.table.updated_at')" sortable="custom" prop="updated_at" width="160">
          <template slot-scope="scope">
            <span>{{ scope.row.updated_at }}</span>
          </template>
        </el-table-column> -->
      <el-table-column
        label="操作时间"
        sortable="custom"
        prop="deleted_at"
        width="160"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.updated_at }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('common.operation')"
        class-name="small-padding fixed-width"
        width="140"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            circle
            @click="handleEdit(scope.row)"
          />
          <el-button
            v-if="scope.row.type == '新闻'"
            type="danger"
            icon="el-icon-delete"
            circle
            @click="handleDelete(scope.row)"
          />
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <div class="pagination-operation">
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        class="pagination"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import { getList, deleteWithId } from "@/api/news";
import waves from "@/directive/waves"; // Waves directive
import Pagination from "@/components/Pagination";

export default {
  name: "MemberList",
  components: { Pagination },
  directives: { waves },
  data() {
    var validateMobile = (rule, value, callback) => {
      if (value === "") {
        callback(
          new Error(
            this.$t("hint.error.please_enter", {
              attribute: this.$t("member.cellphone")
            })
          )
        );
      } else {
        if (!/^\d+$/.test(value)) {
          callback(
            new Error(
              this.$t("hint.error.wrong_format", {
                attribute: this.$t("member.cellphone")
              })
            )
          );
        }
        callback();
      }
    };
    return {
      formLoading: false,
      actionurl: process.env.BASE_API + "uploadPictures",
      uploadData: {},
      pickerOptions: {
        shortcuts: [
          {
            text: this.$t("usuel.latest_week"),
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: this.$t("usuel.last_month"),
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: this.$t("usuel.last_three_months"),
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      },
      imageUrl: "",
      portrait: "",
      tableKey: 0,
      list: null,
      total: 0,
      multipleSelection: [],
      textMap: {
        update: this.$t("common.amend"),
        create: this.$t("common.add")
      },
      listLoading: true,
      state: 0,
      listQuery: {
        title: "",
        type: "news",
        page: 1,
        limit: 10,
        sort: "-id"
      },
      temp: {
        password: "",
        gender: 0,
        name: "",
        cellphone: "",
        nickname: ""
      },
      dialogFormVisible: false,
      dialogStatus: "",
      adminRules: {
        name: [
          {
            required: true,
            message: this.$t("hint.error.please_enter", {
              attribute: this.$t("member.name")
            }),
            trigger: "blur"
          }
        ],
        cellphone: [
          {
            required: true,
            message: this.$t("hint.error.please_enter", {
              attribute: this.$t("member.cellphone")
            }),
            trigger: "blur"
          },
          { validator: validateMobile, trigger: "blur" }
        ],
        password: [
          {
            required: true,
            message: this.$t("hint.error.please_enter", {
              attribute: this.$t("member.password")
            }),
            trigger: "blur"
          }
        ]
      },
      downloadLoading: false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleSelectionChange(val) {
      // 设置全选/全不选数据
      console.log("🚀 ~  val: ", val);
      this.multipleSelection = val;
    },
    handleCheckAllChange() {
      this.$refs.multipleTable.toggleAllSelection();
    },
    handleDelete(row) {
      // 删除
      this.$confirm("是否删除数据", this.$t("common.hint"), {
        confirmButtonText: this.$t("common.confirm"),
        cancelButtonText: this.$t("common.cancel"),
        type: "warning"
      })
        .then(() => {
          // this.formLoading = true
          deleteWithId(row.id).then(response => {
            this.$notify({
              title: this.$t("common.succeed"),
              message: "删除成功",
              type: "success",
              duration: 2000
            });
            this.getList();
          });
        })
        .catch(() => {});
    },
    handleAdd() {
      sessionStorage.clear("formInfo");
      this.$router.push({
        path: "NewsCreate"
      });
    },
    handleEdit(val) {
      sessionStorage.setItem("formInfo", JSON.stringify(val));
      this.$router.push({
        path: "NewsEdit",
        query: {
          id: val.id
        }
      });
    },
    getList() {
      this.listLoading = true;
      getList(this.listQuery).then(response => {
        this.list = response.data.data;
        this.total = response.data.total;
        this.listLoading = false;
      });
    },

    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    sortChange(data) {
      const { prop, order } = data;
      if (order === "ascending") {
        this.listQuery.sort = "+" + prop;
      } else {
        this.listQuery.sort = "-" + prop;
      }
      this.handleFilter();
    },
    resetTemp() {
      this.temp = {
        state: 1,
        password: "",
        gender: 0,
        name: "",
        cellphone: "",
        nickname: ""
      };
    }
  }
};
</script>
<style rel="stylesheet/scss" lang="scss">
.timeInterval {
  top: -4px;
}
.table-expand {
  font-size: 0;
}
.table-expand label {
  width: 90px;
  color: #99a9bf;
}
.table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 80px;
  height: 80px;
  line-height: 80px;
  text-align: center;
}
.avatar {
  width: 80px;
  height: 80px;
  display: block;
}
</style>
